package com.jialiang.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.net.InetAddress;
import java.net.UnknownHostException;


@Slf4j
public class ApplicationLogInfo {

    private static final String url="https://www.jialiang.com";

    public static void info(ConfigurableApplicationContext application) throws UnknownHostException {
        info(application,url);
    }

    public static void info(ConfigurableApplicationContext application,String url) throws UnknownHostException {
        ConfigurableEnvironment env = application.getEnvironment();
        String ip= InetAddress.getLocalHost().getHostAddress();
        String port= env.getProperty("server.port");
        String active= env.getProperty("spring.profiles.active");
        String contextPath= env.getProperty("server.servlet.context-path");
        if (contextPath == null) {
            contextPath = "";
        }
        if (port == null){
            port = "8080";
        }
        if (url == null){
            url = ApplicationLogInfo.url;
        }
        log.info("\n----------------------------------------------------------\n\t" +
                "欢迎访问  \t " + url + "\n\t" +
                "示例程序【" + active + "】环境已启动! 地址如下:\n\t" +
                "Local: \t\t http://localhost:" + port + contextPath + "\n\t" +
                "External: \t http://" + ip + ':' + port + contextPath + '\n' +
                "Swagger文档: \t http://" + ip + ":" + port + contextPath + "/doc.html\n" +
                "----------------------------------------------------------");
    }

}
